package com.zhengdongzulin.zuke.app.main.home.map

import android.content.Intent
import android.os.Bundle
import android.view.Gravity
import android.view.View
import android.view.ViewGroup
import android.view.animation.Animation
import android.widget.EditText
import android.widget.TextView
import androidx.core.widget.addTextChangedListener
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.RecyclerView
import com.baidu.mapapi.map.*
import com.baidu.mapapi.model.LatLng
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.google.android.material.bottomsheet.BottomSheetBehavior
import com.google.android.material.bottomsheet.BottomSheetBehavior.STATE_EXPANDED
import com.google.gson.Gson
import com.lxj.androidktx.core.*
import com.xuexiang.xui.widget.flowlayout.FlowTagLayout
import com.zhengdongzulin.zuke.R
import com.zhengdongzulin.zuke.app.data.*
import com.zhengdongzulin.zuke.app.main.findhouse.ZKRoomDetailsActivity
import com.zhengdongzulin.zuke.base.*
import com.zhengdongzulin.zuke.databinding.ZkActivityMapFindHouseBinding
import com.zhengdongzulin.zuke.ext.*
import razerdp.basepopup.BasePopupWindow
import razerdp.util.animation.AnimationHelper
import razerdp.util.animation.TranslationConfig


/**
 * jeff
 * 地图找房
 * 2021-08-27 13:20:42
 */
class ZKMapFindHouseActivity : BaseActivity<MapFindHouseVm, ZkActivityMapFindHouseBinding>() {

//    113.674899,34.772704

    var mType = 1

    override fun getLayoutResId(): Int = R.layout.zk_activity_map_find_house

    override fun initVM(): MapFindHouseVm = getVM(MapFindHouseVm::class.java)

    override fun initView() {
        var p = mView.llBottomSheet.layoutParams
        p.height = (windowManager.defaultDisplay.height * .7).toInt()
        mView.llBottomSheet.layoutParams = p
        var behavior = BottomSheetBehavior.from(mView.llBottomSheet)
//        setRightButton("测试按钮") {
//            if (behavior.state == STATE_EXPANDED) {
//                //如果是展开状态，则关闭，反之亦然
//                behavior.state = STATE_COLLAPSED
//            } else {
//                behavior.state = STATE_EXPANDED
//            }
//        }
        initHouseTypePopwinndow()
        initPricePopwindow()
        setTitleContent("地图找房")
    }

    var mData: MutableList<MapData3> = mutableListOf()
    lateinit var mAdapter: VListAdpater

    var mData1: MutableList<Sel> = mutableListOf()
    var mData2: MutableList<Sel> = mutableListOf()
    var mData3: MutableList<Sel> = mutableListOf()
    var mData1sel = Sel()
    var mData2sel = Sel()
    var mData3sel = Sel()

    var mHouseType1 = mutableListOf("不限", "一居", "两居", "三居", "三居+")
    var mHouseType2 = mutableListOf("不限", "两居", "三居", "四居", "四居+")
    var mPrice = mutableListOf("不限", "500元以下", "500-1000元", "1000-2000元", "2000-3000元", "3000元以上")
    var mHouseTypeSel = Sel()
    var mDirectionSel = Sel()
    var mPriceSel = Sel()
    var mdirectionData = mutableListOf<Sel>()

    lateinit var mLocationPop: BasePopupWindow
    lateinit var mHouseTypePop: BasePopupWindow
    lateinit var mPricePop: BasePopupWindow
    lateinit var mDirectionPop: BasePopupWindow

    override fun initData() {
        mViewModel.dic_list()
        var city = sp().getString("current_city", "")
        if (mShareViewModel.city.value != null) {
            mViewModel.position_list(mShareViewModel.city.value?.cityId)
            mViewModel.get_geo_house_by_city(mShareViewModel.city.value?.cityId!!)
        } else if (!city.isNullOrEmpty()) {
            var mCity = city.toBean<City>()
            mViewModel.position_list(mCity.cityId)
            mViewModel.get_geo_house_by_city(mCity.cityId)
        } else {
            mViewModel.get_city_com_list()
        }

        mAdapter = VListAdpater(mData)
        mAdapter.setOnItemClickListener { adapter, view, position ->
            Intent().put("id", mData[position].id)
                .start(this, ZKRoomDetailsActivity::class.java)
        }
        mView.shopCarRecyclerView.vertical().adapter = mAdapter

    }

    fun resetFilter() {
        mView.sel1.setDrawRight(R.mipmap.up_triangle_down)
        mView.sel1.setColor(R.color.black_66)
        mView.sel3.setDrawRight(R.mipmap.up_triangle_down)
        mView.sel3.setColor(R.color.black_66)
        mView.sel4.setDrawRight(R.mipmap.up_triangle_down)
        mView.sel4.setColor(R.color.black_66)
    }

    fun getData(lat2: String = "", lng2: String = "", id: String = "", isJi: String = "") {
        var post = FindHousePostData().apply {
            when (mData1sel.name) {
                "区域" -> {
                    if (!mData3sel.name.isNullOrEmpty()) {
                        districtId = mData3sel.id
                    } else {
                        townId = mData2sel.id
                    }
                }
                "地铁" -> {
                    stationId = mData3sel.id
                }
                "附近" -> {
                    nearStatus = "1"
                    nearM = mData2sel.id
                    if (mShareViewModel.mLatLng.value != null) {
                        lat = mShareViewModel.mLatLng.value!!.latitude.toString()
                        lng = mShareViewModel.mLatLng.value!!.longitude.toString()
                    }
                }
                "不限" -> {
                }
            }
            if (!mHouseTypeSel.name.isNullOrEmpty()) {
                houseType = mHouseTypeSel.houseType
                if (mHouseTypeSel.minPrice != 0)
                    roomCount = mHouseTypeSel.minPrice.toString()
            }
            if (!mPriceSel.name.isNullOrEmpty() && mPriceSel.name != "不限") {
                minPrice = mPriceSel.minPrice.toString()
                if (mPriceSel.maxPrice != 0)
                    maxPrice = mPriceSel.maxPrice.toString()
            }
            if (!mDirectionSel.name.isNullOrEmpty()) {
                orientationId = mDirectionSel.id
            }
            if (!lat2.isNullOrEmpty()) {
                lat = lat2
                lng = lng2
            }
            if (!isJi.isNullOrEmpty()) {
                isJizhong = isJi
            }
        }
        var json = Gson().toJson(post)
        val map: Map<String, Any> = HashMap()
        var data: Map<String, Any> = Gson().fromJson(json, map.javaClass)
        mViewModel.mCurrentPage = 1
        when (mType) {
            1 -> {
                mViewModel.get_geo_house_by_city(mShareViewModel.city.value?.cityId!!, data)
            }
            2 -> {
                mViewModel.get_geo_house_by_area(data)
            }
            3 -> {
                mViewModel.get_geo_house_info(mShareViewModel.city.value?.cityId!!, data)
            }
        }
    }

    override fun initListener() {
        mView.bmapView.apply {
            showScaleControl(false)//显示比例尺控件
            showZoomControls(false)//显示缩放级别
            removeViewAt(1)//隐藏logo
        }.map.apply {
            var llCentre = LatLng(34.772704, 113.674899)
            var builder = MapStatus.Builder()
            builder.target(llCentre)//缩放中心点
                .zoom(13f)//缩放级别
            animateMapStatus(MapStatusUpdateFactory.newMapStatus(builder.build()))
            setMaxAndMinZoomLevel(12f, 19f)
            setOnMapStatusChangeListener(object : BaiduMap.OnMapStatusChangeListener {
                override fun onMapStatusChangeStart(p0: MapStatus?) {

                }

                override fun onMapStatusChangeStart(p0: MapStatus?, p1: Int) {
                }

                override fun onMapStatusChange(p0: MapStatus?) {
                }

                override fun onMapStatusChangeFinish(status: MapStatus?) {
//                if (status!!.zoom>=12f&&status!!.zoom<=14f){
                    if (status!!.zoom < 14f) {
                        if (mType != 1) {
                            mView.bmapView.map.clear()
                            mType = 1
                            getData()
                        }
                    } else if (status!!.zoom > 14f && status!!.zoom <= 17f) {
                        if (mType != 2) {
                            mView.bmapView.map.clear()
                            mType = 2
                            getData()
                        }
                    } else if (status!!.zoom > 17f) {
                        if (mType != 3) {
                            mView.bmapView.map.clear()
                            mType = 3
                            getData()
                        }
                    }
                }

            })
            setOnMarkerClickListener {
                if (mType == 1) {
                    mView.bmapView.map.clear()
                    mType = 2
                    val toDouble = it.extraInfo.getString("lng")!!.toDouble()
                    val toDouble1 = it.extraInfo.getString("lat")!!.toDouble()
                    getData(id = it.extraInfo.getString("id")!!)
                    var llCentre = LatLng(toDouble1, toDouble)
                    var builder = MapStatus.Builder()
                    builder.target(llCentre)//缩放中心点
                        .zoom(15f)//缩放级别
                    animateMapStatus(MapStatusUpdateFactory.newMapStatus(builder.build()))
//                } else if (mType == 2||mType == 3) {
                } else {
//                    bmapView.map.clear()
                    mType = 3
//                    val toDouble = it.extraInfo.getString("lng")!!.toDouble()
//                    val toDouble1 = it.extraInfo.getString("lat")!!.toDouble()
                    getData(isJi = it.extraInfo.getString("isJizhong")!!)
                    mView.key.setText(it.extraInfo.getString("key")!!)
                    mView.value.setText(it.extraInfo.getString("value")!!)
//                    var llCentre = LatLng(toDouble1, toDouble)
//                    var builder = MapStatus.Builder()
//                    builder.target(llCentre)//缩放中心点
//                    animateMapStatus(MapStatusUpdateFactory.newMapStatus(builder.build()))
                }
                false
            }
        }
    }

    override fun startObserve() {
        mViewModel.get_city_com_list.observe(this, Observer {
            mViewModel.position_list(it[0].cityId)
            mViewModel.get_geo_house_by_city(it[0].cityId)
        })
        mViewModel.position_list.observe(this, Observer {
            initLocationPopwinndow(it)
        })
        mViewModel.position_list.state.observe(this, getObserver(mViewModel.position_list))
        mViewModel.dic_list.observe(this, Observer {
            mdirectionData = it
            initHdirectionPopwindow()
        })
        mViewModel.dic_list.state.observe(this, getObserver(mViewModel.dic_list))
        mViewModel.get_geo_house_by_city.observe(this, Observer {
            for (record in it) {
                var marker = LatLng(record.lat, record.lng)
                var v = layoutInflater.inflate(R.layout.item_map_data_1, null)
                v.findViewById<TextView>(R.id.tv).setText(record.num)
                val bitmap: BitmapDescriptor = BitmapDescriptorFactory
                    .fromBitmap(v.transViewToBitmap(120, 120))
                var ban = Bundle()
                ban.putString("lat", record.lat!!.toString())
                ban.putString("lng", record.lng!!.toString())
                ban.putString("id", record.id!!.toString())
                val option: OverlayOptions = MarkerOptions()
                    .position(marker)
                    .icon(bitmap)
                    .anchor(0.5f, 0.5f)
                    .extraInfo(ban)
//                val option2: OverlayOptions = TextOptions()
//                    .position(marker)
//                    .text(record.num)
//                    .fontSize(dp2px(14f))
//                    .fontColor(color(R.color.white))
                mView.bmapView.map.addOverlay(option)
//                bmapView.map.addOverlay(option2)
            }
        })
        mViewModel.get_geo_house_by_area.observe(this, Observer {
            for (record in it) {
                var marker = LatLng(record.lat, record.lng)
                var v = layoutInflater.inflate(R.layout.item_map_data_2, null)
                v.findViewById<TextView>(R.id.tv).setText("${record.name} ${record.num}间")
                val bitmap: BitmapDescriptor = BitmapDescriptorFactory
                    .fromBitmap(v.transViewToBitmap(600, 100))
                var ban = Bundle()
                ban.putString("lat", record.lat!!.toString())
                ban.putString("lng", record.lng!!.toString())
                ban.putString("isJizhong", record.isJiZhong!!.toString())
                ban.putString("key", record.name?.let {
                    ""
                })
                ban.putString("value", record.businessDistrictName!!.toString())
                val option: OverlayOptions = MarkerOptions()
                    .position(marker)
                    .icon(bitmap)
                    .anchor(0.5f, 0.5f)
                    .extraInfo(ban)
//                val option2: OverlayOptions = TextOptions()
//                    .position(marker)
//                    .text(record.num)
//                    .fontSize(dp2px(14f))
//                    .fontColor(color(R.color.white))
                mView.bmapView.map.addOverlay(option)
//                bmapView.map.addOverlay(option2)
            }
        })
        mViewModel.get_geo_house_info.observe(this, Observer {
            mData.clear()
            mData.addAll(it)
            mAdapter.notifyDataSetChanged()
            var behavior = BottomSheetBehavior.from(mView.llBottomSheet)
            behavior.state = STATE_EXPANDED
        })
        mViewModel.get_geo_house_by_city.state.observe(this, Observer {

        })
    }

    fun initLocationPopwinndow(datas: MutableList<Sel>? = null) {
        if (datas != null)
            mData1.addAll(datas)
        mData2.clear()
        mData3.clear()
        for (sel in mData1) {
            sel.isSelect = false
        }
        mData1[mData1.size - 1].isSelect = true
        mData1sel = mData1[mData1.size - 1]
        var viewGroup = layoutInflater.inflate(R.layout.popwindow_find_house_sel1, null)
        var rv1 = viewGroup.findViewById<RecyclerView>(R.id.rv1)
        var rv2 = viewGroup.findViewById<RecyclerView>(R.id.rv2)
        var rv3 = viewGroup.findViewById<RecyclerView>(R.id.rv3)
//        viewGroup.findViewById<View>(R.id.cancel).setOnClickListener {
//            if (mLocationPop.isShowing) mLocationPop.dismiss()
//        }
        val selAdpater1 = SelAdpater(mData1)
        rv1.vertical().adapter = selAdpater1
        val selAdpater2 = SelAdpater(mData2)
        rv2.vertical().adapter = selAdpater2
        val selAdpater3 = SelAdpater(mData3)
        rv3.vertical().adapter = selAdpater3
        selAdpater1.setOnItemClickListener { adapter, view, position ->
            for (i in mData1) {
                i.isSelect = false
            }
            val sel = mData1[position]
            sel.isSelect = true
            mData1sel = sel
            selAdpater1.notifyDataSetChanged()
            mData2.clear()
            mData3.clear()
            mData2sel = Sel()
            mData3sel = Sel()
            if (sel.list != null && sel.list!!.size > 0) {
                for (j in sel.list!!) {
                    j.isSelect = false
                }
                val sel1 = sel.list!![0]
                sel1.isSelect = true
                mData2sel = sel1
                mData2.addAll(sel.list!!)
                rv2.smoothScrollToPosition(0)
                if (sel1.list != null && sel1.list!!.size > 0) {
                    for (j in sel1.list!!) {
                        j.isSelect = false
                    }
                    val sel2 = sel1.list!![0]
                    sel2.isSelect = true
                    mData3sel = sel2
                    mData3.addAll(sel1.list!!)
                    rv3.smoothScrollToPosition(0)
                }
            }
            selAdpater2.notifyDataSetChanged()
            selAdpater3.notifyDataSetChanged()
        }
        selAdpater2.setOnItemClickListener { adapter, view, position ->
            for (i in mData2) {
                i.isSelect = false
            }
            val sel = mData2[position]
            sel.isSelect = true
            mData2sel = sel
            selAdpater2.notifyDataSetChanged()
            mData3.clear()
            mData3sel = Sel()
            if (sel.list != null && sel.list!!.size > 0) {
                for (j in sel.list!!) {
                    j.isSelect = false
                }
                val sel1 = sel.list!![0]
                sel1.isSelect = true
                mData3sel = sel1
                mData3.addAll(sel.list!!)
                rv3.smoothScrollToPosition(0)
            }
            selAdpater3.notifyDataSetChanged()
        }
        selAdpater3.setOnItemClickListener { adapter, view, position ->
            for (i in mData3) {
                i.isSelect = false
            }
            val sel = mData3[position]
            sel.isSelect = true
            mData3sel = sel
            selAdpater3.notifyDataSetChanged()
        }
        mLocationPop = object : BasePopupWindow(this) {
            override fun onCreateShowAnimation(): Animation {
                return AnimationHelper.asAnimation()
                    .withTranslation(TranslationConfig.FROM_TOP)
                    .toShow()
            }

            override fun onCreateDismissAnimation(): Animation {
                return AnimationHelper.asAnimation()
                    .withTranslation(TranslationConfig.TO_TOP)
                    .toDismiss()
            }
        }.apply {
            contentView = viewGroup
            setPopupGravity(Gravity.BOTTOM)
            setHeight(ViewGroup.LayoutParams.WRAP_CONTENT)
            setWidth(ViewGroup.LayoutParams.MATCH_PARENT)
            setAlignBackground(true)
        }
        mLocationPop.setOnDismissListener(object : BasePopupWindow.OnDismissListener() {
            override fun onDismiss() {
                if (!mData1sel.name.isNullOrEmpty() && mData1sel.name != "不限") {
                    mView.sel1.setDrawRight(R.mipmap.up_triangle_sel_down)
                    mView.sel1.setColor(R.color.color_1273f7)
                } else {
                    mView.sel1.setDrawRight(R.mipmap.up_triangle_down)
                    mView.sel1.setColor(R.color.black_66)
                }
                getData()
            }

        })
        mView.sel1.setOnClickListener {
            mView.sel1.setDrawRight(R.mipmap.up_triangle_sel)
            mView.sel1.setColor(R.color.color_1273f7)
            dismissPop()
            mLocationPop.showPopupWindow(it)
        }
    }

    fun initHouseTypePopwinndow(type: String = "") {
        var viewGroup = layoutInflater.inflate(R.layout.popwindow_find_house_sel2, null)
        var housetypeData1 = mutableListOf<Sel>()
        var housetypeData2 = mutableListOf<Sel>()
        for (s in mHouseType1.indices) {
            if (s == 0 && type == "整租") {
                housetypeData1.add(Sel(mHouseType1[s], null, true, s, 0, "1"))
                mView.sel2.setDrawRight(R.mipmap.up_triangle_sel_down)
                mView.sel2.setColor(R.color.color_1273f7)
                mHouseTypeSel = housetypeData1[0]
            } else {
                housetypeData1.add(Sel(mHouseType1[s], null, false, s, 0, "1"))
            }
        }
        for (s in mHouseType2.indices) {
            if (s == 0 && type == "合租") {
                mView.sel2.setDrawRight(R.mipmap.up_triangle_sel_down)
                mView.sel2.setColor(R.color.color_1273f7)
                housetypeData2.add(
                    Sel(
                        mHouseType2[s],
                        null,
                        true,
                        (if (s != 0) s + 1 else s),
                        0,
                        "2"
                    )
                )
                mHouseTypeSel = housetypeData2[0]
            } else {
                housetypeData2.add(
                    Sel(
                        mHouseType2[s],
                        null,
                        false,
                        (if (s != 0) s + 1 else s),
                        0,
                        "2"
                    )
                )
            }
        }
        var rv1 = viewGroup.findViewById<RecyclerView>(R.id.rv1)
        var rv2 = viewGroup.findViewById<RecyclerView>(R.id.rv2)
        var tv_empty1 = viewGroup.findViewById<TextView>(R.id.tv_empty1)
        viewGroup.findViewById<View>(R.id.reset).setOnClickListener {
            for (s in housetypeData1) {
                s.isSelect = false
            }
            for (s in housetypeData2) {
                s.isSelect = false
            }
            mHouseTypeSel = Sel()
            rv1.updateData(housetypeData1)
            rv2.updateData(housetypeData2)
        }
//        viewGroup.findViewById<View>(R.id.cancel).setOnClickListener {
//            mHouseTypePop.dismiss()
//        }
        viewGroup.findViewById<View>(R.id.ok).setOnClickListener {
            mHouseTypePop.dismiss()
        }
        rv1.vertical(5).bindData(
            housetypeData1,
            R.layout.item_popwidow_findhouse_sel_house_type
        ) { holder, item, position ->
            val view = holder.getView<TextView>(R.id.name)
            view.text = item.name
            if (item.isSelect) {
                view.setBackgroundResource(R.drawable.item_popwindow_findhouse_sel_house_type_sel)
                view.setTextColor(color(R.color.white))
            } else {
                view.setBackgroundResource(R.drawable.item_popwindow_findhouse_sel_house_type)
                view.setTextColor(color(R.color.gray_99))
            }
        }.itemClick { data: List<Sel>, holder, position ->
            for (s in housetypeData1) {
                s.isSelect = false
            }
            for (s in housetypeData2) {
                s.isSelect = false
            }
            mHouseTypeSel = data[position]
            data[position].isSelect = true
            rv1.updateData(data)
            rv2.updateData(housetypeData2)
        }
        rv2.vertical(5).bindData(
            housetypeData2,
            R.layout.item_popwidow_findhouse_sel_house_type
        ) { holder, item, position ->
            val view = holder.getView<TextView>(R.id.name)
            view.text = item.name
            if (item.isSelect) {
                view.setBackgroundResource(R.drawable.item_popwindow_findhouse_sel_house_type_sel)
                view.setTextColor(color(R.color.white))
            } else {
                view.setBackgroundResource(R.drawable.item_popwindow_findhouse_sel_house_type)
                view.setTextColor(color(R.color.gray_99))
            }
        }.itemClick { data: List<Sel>, holder, position ->
            for (s in housetypeData1) {
                s.isSelect = false
            }
            for (s in housetypeData2) {
                s.isSelect = false
            }
            mHouseTypeSel = data[position]
            data[position].isSelect = true
            rv2.updateData(data)
            rv1.updateData(housetypeData1)
        }
        mHouseTypePop = object : BasePopupWindow(this) {
            override fun onCreateShowAnimation(): Animation {
                return AnimationHelper.asAnimation()
                    .withTranslation(TranslationConfig.FROM_TOP)
                    .toShow()
            }

            override fun onCreateDismissAnimation(): Animation {
                return AnimationHelper.asAnimation()
                    .withTranslation(TranslationConfig.TO_TOP)
                    .toDismiss()
            }
        }.apply {
            contentView = viewGroup
            setPopupGravity(Gravity.BOTTOM)
            setHeight(ViewGroup.LayoutParams.WRAP_CONTENT)
            setWidth(ViewGroup.LayoutParams.MATCH_PARENT)
            setAlignBackground(true)
        }
        mHouseTypePop.setOnDismissListener(object : BasePopupWindow.OnDismissListener() {
            override fun onDismiss() {
                if (!mHouseTypeSel.name.isNullOrEmpty()) {
                    mView.sel2.setDrawRight(R.mipmap.up_triangle_sel_down)
                    mView.sel2.setColor(R.color.color_1273f7)
                } else {
                    mView.sel2.setDrawRight(R.mipmap.up_triangle_down)
                    mView.sel2.setColor(R.color.black_66)
                }
                getData()
            }

        })

        mView.sel2.setOnClickListener {
            mView.sel2.setDrawRight(R.mipmap.up_triangle_sel)
            mView.sel2.setColor(R.color.color_1273f7)
            dismissPop()
            if (mShareViewModel.apartmentType.value == "1") {
                tv_empty1.gone()
                rv2.gone()
            } else {
                tv_empty1.visible()
                rv2.visible()
            }
            mHouseTypePop.showPopupWindow(it)
        }
    }

    fun initPricePopwindow() {
        mPriceSel = Sel()
        var viewGroup = layoutInflater.inflate(R.layout.popwindow_find_house_sel3, null)
        var priceData = mutableListOf<Sel>()
        priceData.add(Sel(mPrice[0], null, true))
        priceData.add(Sel(mPrice[1], null, false, 0, 500))
        priceData.add(Sel(mPrice[2], null, false, 500, 1000))
        priceData.add(Sel(mPrice[3], null, false, 1000, 2000))
        priceData.add(Sel(mPrice[4], null, false, 2000, 3000))
        priceData.add(Sel(mPrice[5], null, false, 3000, 0))
        var rv1 = viewGroup.findViewById<RecyclerView>(R.id.rv1)
        var min = viewGroup.findViewById<EditText>(R.id.min)
        var max = viewGroup.findViewById<EditText>(R.id.max)
        min.addTextChangedListener {
            for (s in priceData) {
                s.isSelect = false
            }
            mPriceSel = Sel()
            rv1.updateData(priceData)
        }
        max.addTextChangedListener {
            for (s in priceData) {
                s.isSelect = false
            }
            mPriceSel = Sel()
            rv1.updateData(priceData)
        }
        viewGroup.findViewById<View>(R.id.reset).setOnClickListener {
            min.setText("")
            max.setText("")
            var sel = priceData[0]
            sel.isSelect = true
            mPriceSel = sel
            rv1.updateData(priceData)
        }
//        viewGroup.findViewById<View>(R.id.cancel).setOnClickListener {
//            mPricePop.dismiss()
//        }
        viewGroup.findViewById<View>(R.id.ok).setOnClickListener {
            mPricePop.dismiss()
        }
        rv1.vertical().bindData(
            priceData,
            R.layout.item_popwidow_findhouse_sel_price
        ) { holder, item, position ->
            val view = holder.getView<TextView>(R.id.name)
            view.text = item.name
            if (item.isSelect) {
                view.setColor(R.color.color_1273f7)
            } else {
                view.setColor(R.color.gray_99)
            }
        }.itemClick { data: List<Sel>, holder, position ->
            for (s in priceData) {
                s.isSelect = false
            }
            mPriceSel = data[position]
            data[position].isSelect = true
            rv1.updateData(data)
        }
        mPricePop = object : BasePopupWindow(this) {
            override fun onCreateShowAnimation(): Animation {
                return AnimationHelper.asAnimation()
                    .withTranslation(TranslationConfig.FROM_TOP)
                    .toShow()
            }

            override fun onCreateDismissAnimation(): Animation {
                return AnimationHelper.asAnimation()
                    .withTranslation(TranslationConfig.TO_TOP)
                    .toDismiss()
            }
        }.apply {
            contentView = viewGroup
            setPopupGravity(Gravity.BOTTOM)
            setHeight(ViewGroup.LayoutParams.WRAP_CONTENT)
            setWidth(ViewGroup.LayoutParams.MATCH_PARENT)
            setAlignBackground(true)
        }
        mPricePop.setOnDismissListener(object : BasePopupWindow.OnDismissListener() {
            override fun onDismiss() {
                if (!mPriceSel.name.isNullOrEmpty() && mPriceSel.name != "不限") {
                    mView.sel3.setDrawRight(R.mipmap.up_triangle_sel_down)
                    mView.sel3.setColor(R.color.color_1273f7)
                } else if (!min.getTextZ().isNullOrEmpty() || !max.getTextZ().isNullOrEmpty()) {
                    mView.sel3.setDrawRight(R.mipmap.up_triangle_sel_down)
                    mView.sel3.setColor(R.color.color_1273f7)
                    mPriceSel = Sel(
                        "", null, false,
                        if (!min.getTextZ().isNullOrEmpty()) min.getTextZ().toInt() else 0,
                        if (!max.getTextZ().isNullOrEmpty()) max.getTextZ().toInt() else 0
                    )
                } else {
                    mView.sel3.setDrawRight(R.mipmap.up_triangle_down)
                    mView.sel3.setColor(R.color.black_66)
                }
                getData()
            }

        })
        mView.sel3.setOnClickListener {
            mView.sel3.setDrawRight(R.mipmap.up_triangle_sel)
            mView.sel3.setColor(R.color.color_1273f7)
            dismissPop()
            mPricePop.showPopupWindow(it)
        }
    }

    fun initHdirectionPopwindow(directionData: MutableList<Sel> = mdirectionData) {
        for (s in directionData) {
            s.isSelect = false
        }
        mDirectionSel = Sel()
        val viewGroup = layoutInflater.inflate(R.layout.popwindow_find_house_sel4, null)
        var rv1 = viewGroup.findViewById<RecyclerView>(R.id.rv1)
        viewGroup.findViewById<View>(R.id.reset).setOnClickListener {
            for (s in directionData) {
                s.isSelect = false
            }
            mDirectionSel = Sel()
            rv1.updateData(directionData)
        }
//        viewGroup.findViewById<View>(R.id.cancel).setOnClickListener {
//            mDirectionPop.dismiss()
//        }
        viewGroup.findViewById<View>(R.id.ok).setOnClickListener {
            mDirectionPop.dismiss()
        }
        rv1.vertical(4).bindData(
            directionData,
            R.layout.item_popwidow_findhouse_sel_house_type
        ) { holder, item, position ->
            val view = holder.getView<TextView>(R.id.name)
            view.text = item.name
            if (item.isSelect) {
                view.setBackgroundResource(R.drawable.item_popwindow_findhouse_sel_house_type_sel)
                view.setTextColor(color(R.color.white))
            } else {
                view.setBackgroundResource(R.drawable.item_popwindow_findhouse_sel_house_type)
                view.setTextColor(color(R.color.gray_99))
            }
        }.itemClick { data: List<Sel>, holder, position ->
            for (s in directionData) {
                s.isSelect = false
            }
            mDirectionSel = data[position]
            data[position].isSelect = true
            rv1.updateData(data)
        }
        mDirectionPop = object : BasePopupWindow(this) {
            override fun onCreateShowAnimation(): Animation {
                return AnimationHelper.asAnimation()
                    .withTranslation(TranslationConfig.FROM_TOP)
                    .toShow()
            }

            override fun onCreateDismissAnimation(): Animation {
                return AnimationHelper.asAnimation()
                    .withTranslation(TranslationConfig.TO_TOP)
                    .toDismiss()
            }
        }.apply {
            contentView = viewGroup
            setPopupGravity(Gravity.BOTTOM)
            setAlignBackground(true)
            setHeight(ViewGroup.LayoutParams.WRAP_CONTENT)
            setWidth(ViewGroup.LayoutParams.MATCH_PARENT)
        }
        mDirectionPop.setOnDismissListener(object : BasePopupWindow.OnDismissListener() {
            override fun onDismiss() {
                if (!mDirectionSel.name.isNullOrEmpty()) {
                    mView.sel4.setDrawRight(R.mipmap.up_triangle_sel_down)
                    mView.sel4.setColor(R.color.color_1273f7)
                } else {
                    mView.sel4.setDrawRight(R.mipmap.up_triangle_down)
                    mView.sel4.setColor(R.color.black_66)
                }
                getData()
            }

        })
        mView.sel4.setOnClickListener {
            mView.sel4.setDrawRight(R.mipmap.up_triangle_sel)
            mView.sel4.setColor(R.color.color_1273f7)
            dismissPop()
            mDirectionPop.showPopupWindow(it)
        }
    }

    data class Sel(
        var name: String = "",
        var list: MutableList<Sel>? = null,
        var isSelect: Boolean = false,
        var minPrice: Int = 0,
        var maxPrice: Int = 0,
        var houseType: String = "",
        var id: String = ""
    )

    inner class VListAdpater(
        list: MutableList<MapData3>
    ) : BaseQuickAdapter<MapData3, BaseViewHolder>(
        R.layout.item_find_house,
        list
    ) {
        override fun convert(holder: BaseViewHolder, item: MapData3) {
            holder.loadImage(R.id.photo, item.picUrl, R.mipmap.error_empty_zheng, 5f)
            holder.setText(R.id.title_name, item.name)
            holder.setText(
                R.id.desc,
                "${item.doorName}\u0020\u0020${item.area}㎡\u0020\u0020${item.orientation}"
            )
            holder.setText(R.id.address, item.address)
            var flow = holder.getView<FlowTagLayout>(R.id.flow)
//            flow.setAdapter(object : DefaultFlowTagAdapter(context) {
//                override fun getLayoutId(): Int {
//                    return R.layout.item_find_house_list_flow
//                }
//            })
//            flow.setItems(item.featureList)
            flow.gone()
            var money = holder.getView<TextView>(R.id.money)
            if (mShareViewModel.apartmentType.value!! == "1") {//人才公寓
                money.text = ""
                money.appendStyleSpan("面议")
            } else {
                money.text = "￥${item.rentPrice}"
                money.appendSizeSpan("/月", 0.46f)
                    .colorSpan(
                        "",
                        money.text.length - 2..money.text.length,
                        color(R.color.black_66)
                    )
                    .styleSpan("", 0..money.text.length - 2)
            }
        }
    }

    fun dismissPop() {
        if (mLocationPop != null && mLocationPop.isShowing)
            mLocationPop.dismiss()
        if (mHouseTypePop != null && mHouseTypePop.isShowing)
            mHouseTypePop.dismiss()
        if (mPricePop != null && mPricePop.isShowing)
            mPricePop.dismiss()
        if (mDirectionPop != null && mDirectionPop.isShowing)
            mDirectionPop.dismiss()
    }

    inner class SelAdpater(
        list: MutableList<Sel>
    ) : BaseQuickAdapter<Sel, BaseViewHolder>(
        R.layout.item_find_house_sel,
        list
    ) {
        override fun convert(holder: BaseViewHolder, item: Sel) {
            val name = holder.getView<TextView>(R.id.name)
            val line = holder.getView<View>(R.id.line)
            if (item.isSelect) {
                name.setTextColor(color(R.color.color_1273f7))
                line.visibility = View.VISIBLE
            } else {
                name.setTextColor(color(R.color.black_66))
                line.visibility = View.INVISIBLE
            }
            name.text = item.name
        }
    }

}